package com.youbo.ysqd.controller;


import com.youbo.ysqd.Config;
import com.youbo.ysqd.event.SearchReceive;
import com.youbo.ysqd.model.Block;
import com.youbo.ysqd.tools.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/***
 *  测试
 * @author macy
 */
@RestController
@RequestMapping("")
public class ApiTestController {

    @Autowired
    RedisCache redisCache;



    @Autowired
    Config config;


    @RequestMapping("/test")
    public Object test() {
        return redisCache.getCacheObject("ysqd_myuser");
    }



    @RequestMapping("/query")
    public Object query() {
        Map<String,Object> map=new HashMap<>();
        Block block=redisCache.getCacheObject("ysqd_myuser");
        map.put("block",block);
        try{
            Map<String,Boolean> idsMap=redisCache.getCacheObject("idsmap");
            if(null!=idsMap){
                int ok=0;
                for (String mkey:idsMap.keySet()){
                    Boolean kv=idsMap.get(mkey);
                    if(null!=kv&&kv){
                        ok++;
                    }
                }
                map.put("qdl","总抢单率"+((Integer)(ok/idsMap.size()*100))+"%");
            }
        }catch (Exception e){}
        return map;
    }

    @RequestMapping("/queryDay")
    public Object queryDay() {

        Block block=redisCache.getCacheObject("ysqd_myuser");
        if(null==block){
            return null;
        }
        String date=new SimpleDateFormat("yyyyMMdd").format(new Date());
        Integer max=redisCache.getCacheObject(date+":max");
        if(max==null){
            return 0;
        }
        return max;
    }


    @RequestMapping("/update")
    public Object update(@RequestBody Block block) {
        Map<String,Object> map=new HashMap<>();
        if(null==block.getCzpwd()){
            map.put("code",500);
            map.put("msg","请输入操作密码");
            return map;
        }

        if(!config.getPassword().equals(block.getCzpwd())){
            map.put("code",500);
            map.put("msg","操作密码不正确");
            return map;
        }

        Block dbl=redisCache.getCacheObject("ysqd_myuser");
        if(null==dbl){
            dbl=SearchReceive.initBlcok();
        }

//        if(block.getStarts()==1){
//            newQdService.newId=null;
//            newQdService.ready=false;
//            newQdService.ready();
//        }


        if(!block.getUsername().equals(dbl.getUsername())){
            SearchReceive.usernames=block.getUsername().split(",");
        }

        dbl.setId("1");
        dbl.setUsername(block.getUsername());
        dbl.setPassword(block.getPassword());
        dbl.setStarts(block.getStarts());
        dbl.setDaymax(block.getDaymax());
        dbl.setKeyWord(block.getKeyWord());
        dbl.setInKeyWord(block.getInKeyWord());




        redisCache.setCacheObject("ysqd_myuser",dbl);
        SearchReceive.block=dbl;

        map.put("code",0);
        map.put("msg","操作成功");

        return map;
    }

    @RequestMapping("/getLogInfo")
    public Object getLogInfo(@RequestParam("name") String name) {
        return readFileContent("/home/ysqd/logs/info/"+name);
    }
    @RequestMapping("/getLogError")
    public Object getLogError(@RequestParam("name") String name) {
        return readFileContent("/home/ysqd/logs/error/"+name);
    }



    public  String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.insert(0,tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
}
